#include "stdint.h"
#include "stdio.h"
#include "sdkconfig.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"

#include "DevicesQueue.h"
#include "DevicesUart.h"
#include "FeedPhy.h"
#include "FeedFrame.h"
#include "FeedPack.h"
#include "mqtt_tcp.h"
#include "gatts_table.h"

#include "taskNetwork.h"


/* 通信消息解析任务句柄 */
TaskHandle_t g_TaskNetworkHand = NULL;


/* 解析缓存 */
static uint8_t st_ucDatas[1024];


void vTaskNetwork(void *pvParameters)
{
    int32_t iLength = 0;

    while (1)
    {
        /* 等待任务消息 */
        ulTaskNotifyTake(pdTRUE, 100 / portTICK_PERIOD_MS);

        /* 读取并解析数据 */
        while((iLength = iQueueGetLengthNeed(&g_TypeQueueUart0Read, '\r')) > 0)
        {
            iLength = (iLength > sizeof(st_ucDatas)) ? sizeof(st_ucDatas) : iLength;
            enumQueuePopDatas(&g_TypeQueueUart0Read, st_ucDatas, iLength);

            cFeedPackAnalysis(FEED_PHY_CHANNEL_UART0, st_ucDatas, iLength);
        }

        /* 发送数据 */
        while((iLength = iQueueGetLength(&g_TypeQueueUart0Send)) > 0)
        {
            iLength = (iLength > sizeof(st_ucDatas)) ? sizeof(st_ucDatas) : iLength;
            enumQueuePopDatas(&g_TypeQueueUart0Send, st_ucDatas, iLength);

            vUartSendDatas(&UART0, st_ucDatas, iLength);
        }

        /* 读取并解析数据 */
        while((iLength = iQueueGetLength(&g_TypeQueueUart1Read)) > 0)
        {
            iLength = (iLength > sizeof(st_ucDatas)) ? sizeof(st_ucDatas) : iLength;
            enumQueuePopDatas(&g_TypeQueueUart1Read, st_ucDatas, iLength);

            cFeedPackAnalysis(FEED_PHY_CHANNEL_UART1, st_ucDatas, iLength);
        }

        /* 发送数据 */
        while((iLength = iQueueGetLength(&g_TypeQueueUart1Send)) > 0)
        {
            iLength = (iLength > sizeof(st_ucDatas)) ? sizeof(st_ucDatas) : iLength;
            enumQueuePopDatas(&g_TypeQueueUart1Send, st_ucDatas, iLength);

            vUartSendDatas(&UART1, st_ucDatas, iLength);
        }

        /* 读取并解析数据 */
        while((iLength = iQueueGetLengthNeed(&g_TypeQueueBleRead, '\r')) > 0)
        {
            iLength = (iLength > sizeof(st_ucDatas)) ? sizeof(st_ucDatas) : iLength;

            memset(st_ucDatas, 0, sizeof(st_ucDatas));
            enumQueuePopDatas(&g_TypeQueueBleRead, st_ucDatas, iLength);

            cFeedPackAnalysis(FEED_PHY_CHANNEL_BLE, st_ucDatas, iLength);
        }

        /* 发送数据 */
        while((iLength = iQueueGetLengthNeed(&g_TypeQueueBleSend, '\r')) > 0)
        {
            iLength = (iLength > (512 - 3)) ? (512 - 3) : iLength;
            enumQueuePopDatas(&g_TypeQueueBleSend, st_ucDatas, iLength);

            ble_datas_send(st_ucDatas, iLength);
        }

        /* 读取并解析数据 */
        while((iLength = iQueueGetLengthNeed(&g_TypeQueueMqttRead, '\r')) > 0)
        {
            iLength = (iLength > sizeof(st_ucDatas)) ? sizeof(st_ucDatas) : iLength;

            memset(st_ucDatas, 0, sizeof(st_ucDatas));
            enumQueuePopDatas(&g_TypeQueueMqttRead, st_ucDatas, iLength);

            cFeedPackAnalysis(FEED_PHY_CHANNEL_MQTT, st_ucDatas, iLength);
        }

        /* 发送数据 */
        while((iLength = iQueueGetLengthNeed(&g_TypeQueueMqttSend, '\r')) > 0)
        {
            iLength = (iLength > sizeof(st_ucDatas)) ? sizeof(st_ucDatas) : iLength;
            enumQueuePopDatas(&g_TypeQueueMqttSend, st_ucDatas, iLength);

            mqtt_datas_send(st_ucDatas, iLength);
        }
    }
}
